package com.learn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.learn.base.ApiResult;
import com.learn.base.ApiResultUtils;
import com.learn.dto.req.AddProductItemReq;
import com.learn.dto.req.QueryProductItemDetailReq;
import com.learn.dto.req.QueryProductItemListReq;
import com.learn.dto.req.UpdateProductItemReq;
import com.learn.dto.resp.QueryProductItemDetailResp;
import com.learn.dto.resp.QueryProductItemListResp;
import com.learn.emuns.LearnResultEnums;
import com.learn.entity.ProductItem;
import com.learn.entity.ProductItemBenefitRef;
import com.learn.repository.ProductItemBenefitRefRepository;
import com.learn.repository.ProductItemMapper;
import com.learn.service.ProductItemService;
import com.learn.utils.GlobalTokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ProductItemServiceImpl extends ServiceImpl<ProductItemMapper, ProductItem> implements ProductItemService
{
	@Autowired
	private ProductItemMapper productItemMapper;
	@Autowired
	private ProductItemBenefitRefRepository productItemBenefitRefRepository;

	@Override
	public ApiResult<Boolean> saveProductItem (AddProductItemReq req)
	{
		//保存产品
		ProductItem productItem = new ProductItem ();
		productItem.setDisplayName (req.getDisplayName ());
		productItem.setInternalName (req.getInternalName ());
		productItem.setCode (req.getCode ());
		productItem.setStartTime (req.getStartTime ());
		productItem.setEndTime (req.getEndTime ());
		productItem.setDescription (req.getDescription ());
		productItem.setPackageType (req.getPackageType ());
		productItem.setPaymentMode (req.getPaymentMode ());
		productItem.setBufferDurationHours (req.getBufferDurationHours ());
		productItem.setBillingCycle (req.getBillingCycle ());
		productItem.setCycleUnit (req.getCycleUnit ());
		productItem.setRetailPrice (req.getRetailPrice ());
		productItem.setMarketPrice (req.getMarketPrice ());
		productItem.setPriceDescription (req.getPriceDescription ());
		productItem.setWechatTemplateId (req.getWechatTemplateId ());
		productItem.setAlipayTemplateId (req.getAlipayTemplateId ());
		productItem.setCreateTime (LocalDateTime.now ());
		productItem.setOperatorId (GlobalTokenUtil.getUserAccount ());
		productItem.setLastModifiedId (GlobalTokenUtil.getUserAccount ());
		int addProductsResult = productItemMapper.insert (productItem);
		if (addProductsResult <= 0)
		{
			return ApiResultUtils.build (LearnResultEnums.ADD_PRODUCTS.getStatus (),
					LearnResultEnums.ADD_PRODUCTS.getErrMsg (), false);
		}

		//保存特权信息
		for (int i = 0; i < req.getBenefitIds ().size (); i++)
		{
			ProductItemBenefitRef productItemBenefitRef = new ProductItemBenefitRef ();
			productItemBenefitRef.setProductItemId (productItem.getId ());
			productItemBenefitRef.setBenefitId (req.getBenefitIds ().get (i));
			productItemBenefitRef.setCreateTime (LocalDateTime.now ());
			productItemBenefitRefRepository.insert (productItemBenefitRef);
		}
		return ApiResultUtils.build (LearnResultEnums.SUCCESS.getStatus (), LearnResultEnums.SUCCESS.getErrMsg (),
				true);

	}

	@Override
	public ApiResult<QueryProductItemDetailResp> queryProductItemDetail (QueryProductItemDetailReq req)
	{
		ProductItem productItem = productItemMapper.selectById (req.getId ());

		if (productItem == null)
		{
			return ApiResultUtils.build (LearnResultEnums.PRODUCTS_NOT_FOUND.getStatus (),
					LearnResultEnums.PRODUCTS_NOT_FOUND.getErrMsg (), null);
		}
		QueryProductItemDetailResp queryProductsDetailResp = new QueryProductItemDetailResp ();
		queryProductsDetailResp.setDisplayName (productItem.getDisplayName ());
		queryProductsDetailResp.setInternalName (productItem.getInternalName ());
		queryProductsDetailResp.setCode (productItem.getCode ());
		queryProductsDetailResp.setStartTime (productItem.getStartTime ());
		queryProductsDetailResp.setEndTime (productItem.getEndTime ());
		queryProductsDetailResp.setDescription (productItem.getDescription ());
		queryProductsDetailResp.setPackageType (productItem.getPackageType ());
		queryProductsDetailResp.setPaymentMode (productItem.getPaymentMode ());
		queryProductsDetailResp.setBufferDurationHours (productItem.getBufferDurationHours ());
		queryProductsDetailResp.setBillingCycle (productItem.getBillingCycle ());
		queryProductsDetailResp.setCycleUnit (productItem.getCycleUnit ());
		queryProductsDetailResp.setRetailPrice (productItem.getRetailPrice ());
		queryProductsDetailResp.setMarketPrice (productItem.getMarketPrice ());
		queryProductsDetailResp.setPriceDescription (productItem.getPriceDescription ());
		queryProductsDetailResp.setWechatTemplateId (productItem.getWechatTemplateId ());
		queryProductsDetailResp.setAlipayTemplateId (productItem.getAlipayTemplateId ());
		return ApiResultUtils.buildSuccess (queryProductsDetailResp);
	}

	@Override
	public ApiResult<Boolean> updateProductItem(UpdateProductItemReq req)
	{
		ProductItem productItem = productItemMapper.selectById (req.getId ());
		if (productItem == null)
		{
			return ApiResultUtils.build (LearnResultEnums.PRODUCTS_NOT_FOUND.getStatus (),
					LearnResultEnums.PRODUCTS_NOT_FOUND.getErrMsg (), null);
		}
		productItem.setDisplayName (req.getDisplayName ());
		productItem.setInternalName (req.getInternalName ());
		productItem.setCode (req.getCode ());
		productItem.setStartTime (req.getStartTime ());
		productItem.setEndTime (req.getEndTime ());
		productItem.setDescription (req.getDescription ());
		productItem.setPackageType (req.getPackageType ());
		productItem.setPaymentMode (req.getPaymentMode ());
		productItem.setBufferDurationHours (req.getBufferDurationHours ());
		productItem.setBillingCycle (req.getBillingCycle ());
		productItem.setCycleUnit (req.getCycleUnit ());
		productItem.setRetailPrice (req.getRetailPrice ());
		productItem.setMarketPrice (req.getMarketPrice ());
		productItem.setPriceDescription (req.getPriceDescription ());
		productItem.setWechatTemplateId (req.getWechatTemplateId ());
		productItem.setAlipayTemplateId (req.getAlipayTemplateId ());
		productItem.setOperatorId (GlobalTokenUtil.getUserAccount ());
		productItem.setLastModifiedId (GlobalTokenUtil.getUserAccount ());
		productItem.setCreateTime (LocalDateTime.now ());
		Integer updateProductsResult = productItemMapper.updateById (productItem);
		if (updateProductsResult <= 0)
		{
			return ApiResultUtils.build (LearnResultEnums.UPDATE_PRODUCTS.getStatus (),
					LearnResultEnums.UPDATE_PRODUCTS.getErrMsg (), false);
		}
		return ApiResultUtils.build (LearnResultEnums.SUCCESS.getStatus (), LearnResultEnums.SUCCESS.getErrMsg (),
				true);
	}

	@Override
	public ApiResult<QueryProductItemListResp> queryProductItemList (QueryProductItemListReq req)
	{
		IPage<ProductItem> page = new Page<> (req.getPageIndex (), req.getPageNum ());
		QueryWrapper<ProductItem> productsQueryWrapper = new QueryWrapper<> ();
		productsQueryWrapper.lambda ()
				.eq (StringUtils.isNotEmpty (req.getStatus ()), ProductItem::getStatus, req.getStatus ())
				.like (StringUtils.isNotEmpty (req.getCode ()), ProductItem::getCode, req.getCode ())
				.like (StringUtils.isNotEmpty (req.getInternalName ()), ProductItem::getInternalName,
						req.getInternalName ());
		Page<ProductItem> page1 = (Page<ProductItem>) productItemMapper.selectPage (page, productsQueryWrapper);
		return null;
	}
}